package com.sixmac.service.impl;

import com.sixmac.controller.common.CommonController;
import com.sixmac.core.Constant;
import com.sixmac.dao.WxUserDao;
import com.sixmac.entity.WxUser;
import com.sixmac.service.AccountService;
import com.sixmac.service.OperaService;
import com.sixmac.service.WxUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/5/19 0019 上午 10:03.
 */
@Service
public class WxUserServiceImpl implements WxUserService {

    @Autowired
    private WxUserDao wxUserDao;

    @Autowired
    private AccountService accountService;

    @Autowired
    private OperaService operaService;

    @Override
    public List<WxUser> findAll() {
        return wxUserDao.findAll();
    }

    @Override
    public Page<WxUser> find(int pageNum, int pageSize) {
        return wxUserDao.findAll(new PageRequest(pageNum - 1, pageSize, Sort.Direction.DESC, "id"));
    }

    @Override
    public Page<WxUser> find(int pageNum) {
        return find(pageNum, Constant.PAGE_DEF_SIZE);
    }

    @Override
    public WxUser getById(int id) {
        return wxUserDao.findOne(id);
    }

    @Override
    public WxUser deleteById(int id) {
        WxUser wxUser = getById(id);
        wxUserDao.delete(wxUser);
        return wxUser;
    }

    @Override
    public WxUser create(WxUser wxUser) {
        return wxUserDao.save(wxUser);
    }

    @Override
    public WxUser update(WxUser wxUser) {
        return wxUserDao.save(wxUser);
    }

    @Override
    @Transactional
    public void deleteAll(int[] ids) {
        for (int id : ids) {
            deleteById(id);
        }
    }

    @Override
    public List<WxUser> findListNew() {
        return wxUserDao.findListNew(CommonController.getOldDate());
    }

    @Override
    public List<WxUser> findList(String nickName) {
        nickName = "%" + nickName + "%";
        return wxUserDao.findList(nickName);
    }

    @Override
    public Page<WxUser> page(final String nickname, Integer pageNum, Integer pageSize) {
        Page<WxUser> page = wxUserDao.findAll(new Specification<WxUser>() {
            @Override
            public Predicate toPredicate(Root<WxUser> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate result = null;
                List<Predicate> predicateList = new ArrayList<Predicate>();

                if (StringUtils.isNotEmpty(nickname)) {
                    Predicate pre = cb.like(root.get("nickname").as(String.class), "%" + nickname + "%");
                    predicateList.add(pre);
                }

                if (predicateList.size() > 0) {
                    result = cb.and(predicateList.toArray(new Predicate[]{}));
                }

                if (result != null) {
                    query.where(result);
                }
                return query.getGroupRestriction();
            }

        }, new PageRequest(pageNum - 1, pageSize, Sort.Direction.DESC, "id"));

        return page;
    }

    @Override
    public List<Map<String, Object>> pageToExcel(HttpServletRequest request, Map<String, Object> params) {
        List<WxUser> wxUserList = wxUserDao.findAll();
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = null;

        for (WxUser wxUser : wxUserList) {
            map = new HashMap<String, Object>();
            map.put("nickname", wxUser.getNickname());
            map.put("attentionTime", wxUser.getAttentionTime().toString().substring(0, wxUser.getAttentionTime().toString().length() - 2));
            map.put("count", accountService.findListById(wxUser.getId(), Constant.ACCOUNT_STATUS_WeiXin).size());

            list.add(map);
        }

        operaService.addOperaInfo(request, "导出微信用户数据");

        return list;
    }
}